// pages/order/order.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navData: [{
      name: "首页", //文本
      index: 0, //是否是当前页，0不是  1是
      ico: '../images/tabbar/home-sy', //不同图标
      fn: 'gotoIndex', //对应处理函数
      width: 46,
      height: 46,
    }, {
      name: "订单",
      index: 1,
      ico: '../images/tabbar/home-dd',
      fn: 'gotoOrder',
      width: 44,
      height: 44,
    }, {
      name: "自提",
      index: 2,
      ico: '../images/tabbar/home-zt',
      fn: 'gotoSelfhelp',
      width: 44,
      height: 44,
      zitinum: ''
    }, {
      name: "我的",
      index: 3,
      ico: '../images/tabbar/home-wd',
      fn: 'gotoMine',
      width: 40,
      height: 44,
    }, ],
    current: 1,
    select: -1,
    
    nr: "",
    quxiaoindex: "",
    maxnum:"",
    shoplist: '',
    gengduo:true,
    currentpage:0,
    jiazaizhong:false
  },
  toorderdetails: function(e) {
    var index = e.currentTarget.dataset.index
    wx.navigateTo({
      url: '/pages/orderdetail/orderdetail?orderid=' + this.data.shoplist[index].ID,
    })
  },
  tihuoma: function(e) {
    var index = e.currentTarget.dataset.index
    wx.reLaunch({
      url: '/pages/selfhelp/selfhelp?ordercode=' + this.data.shoplist[index].iCode,
    })
  },
  gotoIndex: function() {
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },
  gotoOrder: function() {
    wx.reLaunch({
      url: '/pages/order/order',
    })
  },
  gotoSelfhelp: function() {
    wx.reLaunch({
      url: '/pages/selfhelp/selfhelp',
    })
  },
  gotoMine: function() {
    wx.reLaunch({
      url: '/pages/mine/mine',
    })
  },
  getselfhelplist: function() {
    ajax('/api/OrderALLORCode/Get?requestContent=' + JSON.stringify({

      token: wx.getStorageSync("Access_token")
    }), {
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      console.log(res)
      this.setData({
        zitilist: res.Models
      })

    })
  },


  showyuanyin: function(e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      // showyuanyin: true,
      quxiaoindex: index
    })

    this.quxiaoorder()
  },

  lower: function() {
    if (this.data.jiazaizhong) {
      return
    }
    if(this.data.gengduo){
      this.setData({
        jiazaizhong: true,
        currentpage:this.data.currentpage+1
      })
      this.getlist(this.data.select, this.data.currentpage * 10)
    }else{
      return
    }

  },

  inputnum: function(e) {
    // console.log(e.detail.value)
    var zishu = e.detail.value.length
    this.setData({
      zishu: zishu,
      nr: e.detail.value
    });
  },

  tcqxdd: function() {
    this.setData({
      showyuanyin: false
    })
  },


  quxiaoorder: function() {

    var index = this.data.quxiaoindex

    console.log(index)

    // this.setData({
    //   showyuanyin: false
    // })

    ajax('/api/OrderCareful/GetCancelOrder?requestContent=' + JSON.stringify({
      token: wx.getStorageSync('Access_token'),
      OrderID: this.data.shoplist[index].ID,
      iCode: this.data.shoplist[index].iCode,
      lbltext: this.data.nr,
      BuildFlag: this.data.shoplist[index].BuildFlag
    }), {
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      console.log(res)
      if (res.Status == 1) {

        wx.showToast({
          title: '提交成功',
          icon: 'none',
          duration: 2000
        })

        this.setData({
          currentpage: 0,
          shoplist:[]
        })

        this.getlist(this.data.select, 0)
      
      }
    })
  },


  topay: function(e) {
    var index = e.currentTarget.dataset.index
    wx.navigateTo({
      url: '/pages/pay/pay?orderid=' + this.data.shoplist[index].ID + '&ordercode=' + this.data.shoplist[index].iCode + '&OrderType=' + this.data.shoplist[index].VType,
    })
  },

  selecttab: function(e) {
    var select = e.currentTarget.dataset.index;
    this.setData({
      select: select,
      currentpage:0,
      gengduo:true,
      jiazaizhong: false,
      shoplist:[]
    })

    ajax('/api/OrderCareful/getConsumer?requestContent=' + JSON.stringify({
      token: wx.getStorageSync('Access_token'),
      PersonID: 0,
      PageIndex: 0,
      BuildFlag: this.data.select
    }), {
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      // console.log(res)
      this.setData({
        shoplist: res.Models
      })

    })
  },
  //获取全部订单列表
  getlist: function(lei, PageIndex) {
    ajax('/api/OrderCareful/getConsumer?requestContent=' + JSON.stringify({
      token: wx.getStorageSync('Access_token'),
      PersonID: this.data.PersonID,
      PageIndex: PageIndex,
      BuildFlag: lei
    }), {
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {

      console.log(res.Models)

      // var list = []
      // for (var x in res.Models) {
      //   if (res.Models[x].BuildFlag == 0) {
      //     var timelist = res.Models[x].DJS.split(":")
      //     console.log(timelist)
      //     res.Models[x].shi = Number(timelist[0])
      //     res.Models[x].fen = Number(timelist[1])
      //     res.Models[x].miao = Number(timelist[2])
      //     list.push(x)
      //   }
      // }

      // console.log(res)

     
      if (PageIndex == 0) {
        

        if (res.Models != null) {
          this.setData({
            shoplist: res.Models,
            maxnum : res.Models[0].Sum
          })
          if (res.Models.length==10){
            if (res.Models[0].Sum>10){
              this.setData({
                gengduo:true
              })
            
            }else{
              this.setData({
                gengduo: false
              })
            }
          }else{
            this.setData({
              gengduo: false
            })
          }

        } else {
          this.setData({
            shoplist: null,
            gengduo: false
          })
        }
      } else {
        

        var add = res.Models
        var shoplist = this.data.shoplist
        for (var x in add){
          shoplist.push(add[x])
        }

        this.setData({
          shoplist: shoplist,
          maxnum: res.Models[0].Sum
        })

        if (res.Models.length == 10) {
          if (res.Models[0].Sum > shoplist.length) {
            this.setData({
              gengduo: true
            })
          } else {
            this.setData({
              gengduo: false
            })
          }
        }else{
          this.setData({
            gengduo: false
          })
        }
      }


      if (this.data.jiazaizhong) {
        this.setData({
          jiazaizhong: false
        })
      }

      // this.settime(list)
    })
  },

  // settime: function(list) {


  //   console.log(list)
  //   // var timerlist = []
  //   var that=this
  //   var list=list
  //   this.setData({
  //     list:list
  //   })
  //   // var flage=true

  //   var timer = setInterval(function () {

  //     var list=that.data.list
  //     // console.log(list)
  //     for (var x in list) {
  //       var index = list[x]

  //       // console.log(index)

  //       var info = that.data.shoplist[index]
  //       // console.log(info)

  //       var miao = info.miao -= 1
  //       var fen = info.fen
  //       var shi = info.shi
  //       if (miao <= 0) {
  //         fen -= 1
  //         miao = 60
  //       }
  //       if (fen == -1) {
  //         shi -= 1
  //         fen = 60
  //       }
  //       // console.log(index)
  //       var str1 = "shoplist[" + index +"].miao"
  //       var str2 = "shoplist[" + index + "].fen"
  //       var str3 = "shoplist[" + index + "].shi"

  //       // console.log(str1, str2, str3)

  //       that.setData({
  //         [str1]: miao,
  //         [str2]: fen,
  //         [str3]: shi
  //       })
  //       if (shi == -1) {
  //         // clearInterval(that.timerlist[x])
  //         console.log("计时结束")
  //         // that.sleep(3000)
  //       }
  //     }



  //     }, 1000)},



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var index = options.index
    if (index) {
      this.setData({
        select: index
      })
    }
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#000',
    })
    if (index) {
      this.setData({
        currentpage: 0
      })
      this.getlist(index, 0)
     
    } else {
      this.setData({
        currentpage: 0
      })
      this.getlist(-1, 0)
     
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getselfhelplist()
    this.setData({
      currentpage: 0
    })
    // this.getlist(-1, 0)
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})